import fs from 'node:fs'
import chalk from 'chalk'
import init from './model/init.js'

const files = fs.readdirSync('./plugins/fisticuffs-plugin/app').filter(file => file.endsWith('.js'))

let begin = Date.now()
let ret = []
let apps = {}

files.forEach((file) => {
    ret.push(import(`./app/${file}`))
})

ret = await Promise.allSettled(ret)

for (let i in files) {
    let name = files[i].replace('.js', '')

    if (ret[i].status !== 'fulfilled') {
        logger.error(`互殴插件载入错误：${logger.red(name)}`)
        logger.error(ret[i].reason)
        continue
    }
    apps[name] = ret[i].value[Object.keys(ret[i].value)[0]]
}

await init.config()

logger.mark(chalk.rgb(123, 255, 105)('---------------------------------------'))
logger.mark(chalk.rgb(123, 255, 105)(`互殴插件载入成功╯‵□′)╯︵┻━┻ 耗时:${Date.now() - begin}ms`))
logger.mark(chalk.rgb(123, 255, 105)('---------------------------------------'))

export {apps}
